<?php
/**
 * XmuSlh, web platform of Student Association Federation of Xiamen University
 * 
 * Rewritting one or some pages of StatusNet. 
 * To get the original version, please visit <http://status.net/>.
 * 
 * StatusNet, the distributed open-source microblogging tool
 * 
 * LICENCE: This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * The EventNews class for table 'eventnews'.
 * 
 * @author		ytukie <ytukie@gmail.com>
 * @version		0.3
 * 
 * 0.2)  Inheriting from BaseNews instead of Original DataObject.
 * 0.3)  Adds the 'tagStarttime' and 'tagEndtime' functions.
 */

// XmuSlh only.
if ( !defined('XMUSLH')) { exit(1); }

// Requirements.
require_once INSTALLDIR . '/lib/slh_basenews.php';

// Definitions.
define('EVENTNEWS_MESSAGE_MAXLENGHT',			200);

class EventNews extends BaseNews
{
	public $__table = 'eventnews';			// table name
	public $id;								// int(11)  primary_key not_null
	public $news_id;						// int(11)  foreign_key unique not_null
	public $user_id;						// int(11)  foreign_key not_null
	public $association_ids;				// varchar(255)
	public $title;							// varchar(255)
	public $message;						// text
	public $rendered;						// text
	public $poster_id;						// int(11)
	public $started;						// datetime()
	public $ended;							// datetime()
	public $address;						// varchar(255)
	public $host;							// varchar(255)
	public $viewcnt;						// int(11)
	public $created;						// datetime()  not_null
	
	// Static get
	function staticGet($key, $value = null)
	{
		return Memcached_DataObject::staticGet('EventNews', $key, $value); 
	}
	
	/**
	 * Delete the event news and its realted base news.
	 * Delete poster.
	 */
	function delete()
	{
		$poster = $this->getPoster();
		if ( !empty($poster)) {
			$poster->delete();
		}
		
		parent::delete();
	}
	
	/**
	 * Get the related poster object if exsits.
	 */
	function getPoster()
	{
		return Poster::staticGet('news_id', $this->news_id);
	}
	
	/**
	 * Increase the view count of the event news by 1.
	 */
	function increaseViewCount()
	{
		$query = 'UPDATE `eventnews` SET `viewcnt` = `viewcnt`+1 ' .
				 'WHERE `news_id` = ' . $this->news_id;
		
		$eventnews = new EventNews();
		return $eventnews->query($query);
	}

	/**
	 * Add the event news into database.
	 * 
	 * $fields includes: 
	 *  $association_ids
	 *  $poster_id
	 *  $title
	 *  $message
	 *  $host
	 *  $address
	 *  $started
	 *  $ended
	 *  $rendered
	 *  $user_id
	 * 
	 * Check the $poster_id if the object already belong to some news_id.
	 */
	static function add($fields)
	{
		// Magically put fields into current scope.
		extract($fields);
		
		$news = new News();
		$news->newstype = NEWS_NEWSTYPE_EVENTNEWS;
// TODO: The other variables.
		$news->newstag_id = 0;
		$news->status = 0;
		
		$news_id = $news->insert();
		if (empty($news_id)) {
			slh_log_db_error($news, 'INSERT', __FILE__);
			return false;
		}
		$news->id = $news_id;
		
		$eventnews = new EventNews();
		$eventnews->news_id = $news_id;
		$eventnews->user_id = $user_id;
		
		// Check the poster_id if it's abusive. (If its 'news_id' is set.)
		if ( !Poster::checkIfSetNewsid($poster_id)) {
			$eventnews->poster_id = $poster_id;
		}
		
		$eventnews->title = $title;		
		$eventnews->message = $message;		
		$eventnews->host = $host;		
		$eventnews->address = $address;		
		$eventnews->started = $started;		
		$eventnews->ended = $ended;

		// FIXME: The html style handler?
		if ($rendered != '')  {
			$eventnews->rendered = $rendered;
		}
		
		$eventnews->created = slh_sql_now();
		
		$eventnews_id = $eventnews->insert();
		if (empty($eventnews_id)) {
			slh_log_db_error($eventnews, 'INSERT', __FILE__);
			
			@$news->delete();
			return false;
		}
		
		// Set the news_id of the given poster item.
		if ( !empty($eventnews->poster_id)) {
			Poster::setNewsidById($eventnews->news_id, $eventnews->poster_id);
		}
		
		return $eventnews_id;
	}
	
	/**
	 * Get the ids as a list. Use offset and limit to control
	 * the pagination.
	 * 
	 * Returning the 'news_id', not the original 'id'.
	 */
	static function getNewsidsByList($offset = 0,
									 $limit = 0)
	{
		return parent::getNewsidsByList(new self(), $offset, $limit);
	}
	
	/**
	 * Get the event news objects by the given newsids.
	 */
	static function getEventnewsByNewsids($newsids)
	{
		return parent::getSubnewsByNewsids('EventNews', $newsids);
	}
	
	/**
	 * To judge that if it is started. And some tags.
	 */
	function tagStarttime()
	{
		$started = strtotime($this->started);
		$ended = strtotime($this->ended);
		$now = time();
		
		if ($now < $started) {
			return ' #未开始';
		} else if ($now >= $started &&
				   $now <= $ended) {
			return ' #正在进行'; 
		}
		
		return '';
	}
	
	/**
	 * To judge that if it is ended.
	 */
	function tagEndtime()
	{
		$started = strtotime($this->started);
		$ended = strtotime($this->ended);
		$now = time();
		
		if ($now > $ended) {
			return ' #已结束'; 
		}
		
		return '';
	}
}
?>